package com.onehash.onebilling.util;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.onehash.onebilling.gui.OneBillingApp;

public class TransactionFileGenerator {

	/**
	 * Author Xia Xi
	 */
	private static Random ran = new Random();

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
	
	public static void main(String[] args) {
		
		String fileName = "data\\transaction.csv";
		String delimiter = ",";
	
		
		/*
		int customerNo = 5;
		String [] customerAcc = new String [customerNo];
		
		for (int n = 0; n < customerNo; n++) {
			if (n <10){
				customerAcc [n] = "CA000"+n;
			}else if (n <100) {
				customerAcc [n] ="CA00"+n;
				
			}else if (n<1000){
				customerAcc[n] ="CA0"+n;
			}
		}
		*/
		
		String [] customerAcc = {"CA0001", "CA0005", "CA0008"}; //hardcode your account number here
		String [] planType ={"Digital","Moblie"};
		String [] callType ={"Local","IDD","Roam"};
		String [] countryCode ={"0060","0062","0063","0066","0081",	"0082",	"0086",	"00886","0091",	"0092",	"0044",	"001","0061","0064"};
		String [] localStartDigit = {"6", "9"};
				
		int aveCustomerMonthlyRecord = 200; // how many average monthly record you want to generate
		
		int totalRecord = aveCustomerMonthlyRecord*14*customerAcc.length;
		//from last year Jan to this year Feb , total 14 months
		
		try {
				  FileOutputStream fostream = new FileOutputStream(fileName);
				  PrintStream p = new PrintStream( fostream );
				  
				 for( int n = 0; n< totalRecord; n++){
					 String ranCallType = getRandomCallType(callType);
					 
					//Account,yyyyMMdd_HHmmss,Digital,IDD,sec,countryCode_number
					 String strLine = getRandomAccount(customerAcc)+ delimiter+ getRandomDateTime() +delimiter+getRandomPlanType(planType)+delimiter+ranCallType+delimiter+
							 getRandomDuration() +delimiter+ getRandomNumberCalled(ranCallType, countryCode,localStartDigit);
					  p.println(strLine);
				 }
				
					 
				//fileclose
				p.close();
				fostream.close();
				System.out.println("Generated total "+ totalRecord +" Transaction Records to "+fileName);
					 
			
		}catch (IOException ioe){
			 ioe.printStackTrace();
		 }

	}
	
	
	public static String getRandomAccount (String [] customerAcc){
		int roll = ran.nextInt(customerAcc.length);
		return customerAcc[roll];
	}
	public static String getRandomPlanType(String [] planType){
		int roll = ran.nextInt(planType.length);
		return planType[roll];
	}
	public static String getRandomCallType(String [] callType){
		int roll = ran.nextInt(callType.length);
		return callType[roll];
	}
	
	public static String getRandomDateTime(){ // yyyyMMdd_HHmmSS from 20110101 to 20120229
		try {
			Date start = sdf.parse("20110101_000000");
			Date end = sdf.parse("20120229_235959");
			long date = random (start.getTime(), end.getTime());
			Date ranDate = new Date(date);
			return sdf.format(ranDate);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "20120203_123456"; //default
	}
	private static long random(long begin,long end){
		long rtn = begin + (long)(Math.random() * (end - begin));

		if(rtn == begin || rtn == end){
			return random(begin,end);
		}
		return rtn;
	}
	
	public static String getRandomNumberCalled(String ranCallType, String [] countryCode, String [] localStartDigit) {
		if ("Local".equalsIgnoreCase(ranCallType)){
			int roll = ran.nextInt(localStartDigit.length);
			return localStartDigit[roll]+ (1000000 + ran.nextInt(899999))+"";
			
		}else{
			int roll = ran.nextInt(countryCode.length);
			return countryCode[roll]+ "-" + (10000000 + ran.nextInt(8999999));
		}
		
	}
	
	
	
	public static int getRandomDuration(){
		int roll = 15 + ran.nextInt(1200); //set average 20mins
		return roll;
	}
	
	
	

}


//TODO HashMap <accountNum+month+planType+callType, ArrayList<transaction>>
//when create transaction, validate month,  and accountNumber and callType and planType not null
//getTotalTxnSec (accountNum, month, planType, callType){}
//getAllTxn (accountNum, month, planType, callType){}